package com.syj.collect.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.syj.collect.Present.AllFragmentPresent;
import com.syj.collect.adapter.AdapterProjectAllFragment;
import com.syj.collect.adapter.MyCollectAdapter;
import com.syj.collect.app.R;
import com.syj.collect.base.BaseFragment;
import com.syj.collect.base.EmptyRecyclerView;
import com.syj.collect.entry.AllFragmentBean;
import com.syj.collect.entry.MyCollectBean;
import com.syj.collect.view.AllFragmentView;

import java.util.List;

public class CheckedFragment extends BaseFragment implements AllFragmentView {

    private AllFragmentPresent present;
    private AdapterProjectAllFragment myCollectAdapter;

    private SmartRefreshLayout refresh;
    private int cruntPage = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.fargment_submit;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        present = new AllFragmentPresent();
        present.attach(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        EmptyRecyclerView rec = rootView.findViewById(R.id.com_rerec);
        rec.setEmptyView(rootView.findViewById(R.id.empty_view_));
        rec.setLayoutManager(new LinearLayoutManager(getContext()));
        myCollectAdapter = new AdapterProjectAllFragment(0);
        rec.setAdapter(myCollectAdapter);


        refresh = rootView.findViewById(R.id.comm_refresh);
        refresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                present.getTaskList(2, ++cruntPage);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                cruntPage = 1;
                present.getTaskList(2, cruntPage);
            }
        });
//        refresh.autoRefresh();

        return rootView;
    }

    @Override
    protected void onVisible() {
        super.onVisible();
        present.getTaskList(2, 1);
    }

    @Override
    public void initSuccess(int status, String str) {


    }

    @Override
    public void initTaskSuccess(int status, int page, List<AllFragmentBean.DataBean.ItemBean> str) {
        if (status == 2 && !str.isEmpty()) {
            if(page==1)myCollectAdapter.ClearData();
            myCollectAdapter.upData(str);
            cruntPage = page;
        }
        if(refresh!=null)refresh.finishLoadMore();
        if(refresh!=null)refresh.finishRefresh();
    }

    @Override
    public void initFail(String str) {
        if(refresh!=null)refresh.finishLoadMore();
        if(refresh!=null)refresh.finishRefresh();
    }

}
